﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography.X509Certificates;
using System.Net;
using System.IO;
using System.Net.Security;
using System.Threading;
using CTIClassLibrary;
using System.Xml;

namespace CTIPay
{
    public partial class FrmCITIC : Form
    {
        public FrmCITIC()
        {
            InitializeComponent();
        }

        private bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            //Return True to force the certificate to be accepted. 
            //just like pressing the OK button in the Browser
            return true;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            this.btnLogin.Enabled = false;

            Thread th = new Thread(new ThreadStart(Login));
            th.Start();
        }

        void Login()
        {
            BANK_CITIC citic = new BANK_CITIC();
            citic.InitializeSecurity();
            citic.Login();
            BeginInvoke(new SetMessageDelegate(SetMessage), MessageType.Request, citic.XMLSent);

            if(string.IsNullOrEmpty(citic.ErrorMsg))
            {
                Invoke(new SetMessageDelegate(SetMessage), MessageType.Response, citic.XMLReceived);
            }
            else
            {
                Invoke(new SetMessageDelegate(SetMessage), MessageType.Response, citic.ErrorMsg);
            }

            Invoke(new EnableButtonDelegate(EnableButton), btnLogout);
        }

        void Logout()
        {
            BANK_CITIC citic = new BANK_CITIC();
            citic.Logout();
            BeginInvoke(new SetMessageDelegate(SetMessage), MessageType.Request, citic.XMLSent);

            if (string.IsNullOrEmpty(citic.ErrorMsg))
            {
                Invoke(new SetMessageDelegate(SetMessage), MessageType.Response, citic.XMLReceived);
            }
            else
            {
                Invoke(new SetMessageDelegate(SetMessage), MessageType.Response, citic.ErrorMsg);
            }

            Invoke(new EnableButtonDelegate(EnableButton), btnLogin);
        }

        enum MessageType
        {
            Request, Response
        }

        delegate void SetMessageDelegate(MessageType type, string msg);
        delegate void ScrollToBottomDelegate();
        delegate void EnableButtonDelegate(Button b);

        /// <summary>
        /// 往来信息的记录
        /// </summary>
        /// <param name="type"></param>
        /// <param name="msg"></param>
        void SetMessage(MessageType type, string msg)
        {
            if (type == MessageType.Request)
            {
                //RTFDocument r = new RTFDocument();
                //string text = ">>> " + DateTime.Now.ToString();
                //r.AppendText(text, 18, Color.Red, "宋体", false, false, false, true);

                //text = msg;
                //r.AppendText(text, 18, Color.Black, "宋体", false, false, false, true);

                //this.rtbRequest.Rtf = r.RTFSource;
                this.rtbRequest.Text += ">>> " + DateTime.Now.ToString() + System.Environment.NewLine;
                this.rtbRequest.Text += "    " + msg + System.Environment.NewLine;
            }
            else
            {
                this.rtbResponse.Text += ">>> " + DateTime.Now.ToString() + System.Environment.NewLine;
                this.rtbResponse.Text += "    " + msg + System.Environment.NewLine;
            }

            ScrollToBottom();
        }

        /// <summary>
        /// 自动滚动文本框到底部
        /// </summary>
        void ScrollToBottom()
        {
            rtbRequest.SelectionStart = rtbRequest.Text.Length;
            rtbRequest.ScrollToCaret();

            rtbResponse.SelectionStart = rtbResponse.Text.Length;
            rtbResponse.ScrollToCaret();
        }

        void EnableButton(Button b)
        {
            b.Enabled = true;
        }

        private void btnLogout_Click(object sender, EventArgs e)
        {
            this.btnLogout.Enabled = false;
            Thread th = new Thread(new ThreadStart(Logout));
            th.Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            WebService.TicketWebService tws = new WebService.TicketWebService();
            string xml = tws.PaymentAmount(textBox1.Text.Trim());
            this.rtbResponse.Text += xml;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            WebService.TicketWebService tws = new WebService.TicketWebService();
            string[] a = textBox2.Text.Trim().Split(',');
            string ret = tws.PaymentChecked(int.Parse(a[0]), decimal.Parse(a[1]));
            this.rtbResponse.Text += "------------" + ret;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            BANK_CITIC citic = new BANK_CITIC();
            citic.PaymentReversal(this.textBox3.Text.Trim());

            SetMessage(MessageType.Request, citic.XMLSent);
            SetMessage(MessageType.Response, citic.XMLReceived);
            SetMessage(MessageType.Response, citic.ErrorMsg);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            BANK_CITIC citic = new BANK_CITIC();
            citic.PaymentRefund(textBox4.Text.Trim(), decimal.Parse(textBox5.Text.Trim()));

            SetMessage(MessageType.Request, citic.XMLSent);
            SetMessage(MessageType.Response, citic.XMLReceived);
            SetMessage(MessageType.Response, citic.ErrorMsg);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            BANK_CITIC citic = new BANK_CITIC();
            citic.GetQuestResult(textBox6.Text.Trim(), dtpStart.Value, dtpEnd.Value);

            SetMessage(MessageType.Request, citic.XMLSent);
            SetMessage(MessageType.Response, citic.XMLReceived);
            SetMessage(MessageType.Response, citic.ErrorMsg);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            BANK_CITIC citic = new BANK_CITIC();
            citic.CheckAccount(textBox7.Text.Trim());

            SetMessage(MessageType.Request, citic.XMLSent);
            SetMessage(MessageType.Response, citic.XMLReceived);
            SetMessage(MessageType.Response, citic.ErrorMsg);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            BANK_CITIC citic = new BANK_CITIC();
            citic.ChangePassword(txtPassword.Text.Trim());

            SetMessage(MessageType.Request, citic.XMLSent);
            SetMessage(MessageType.Response, citic.XMLReceived);
            SetMessage(MessageType.Response, citic.ErrorMsg);
        }

        private void btnSettle_Click(object sender, EventArgs e)
        {
            string batch = txtSettle.Text.Trim();
            BANK_CITIC citic = new BANK_CITIC();
            citic.Settlement(batch);

            SetMessage(MessageType.Request, citic.XMLSent);
            SetMessage(MessageType.Response, citic.XMLReceived);
            SetMessage(MessageType.Response, citic.ErrorMsg);
        }
    }
}
